package pl.edu.wsinf.lukaszwalczak.emailclient.controller;

import java.util.Map;
import javax.inject.Inject;
import org.springframework.stereotype.Component;
import pl.edu.wsinf.lukaszwalczak.emailclient.pojo.Account;
import pl.edu.wsinf.lukaszwalczak.emailclient.pojo.Account.IncomingProtocol;
import pl.edu.wsinf.lukaszwalczak.emailclient.pojo.Account.OutgoingProtocol;
import pl.edu.wsinf.lukaszwalczak.emailclient.pojo.Account.ServerAuth;
import pl.edu.wsinf.lukaszwalczak.emailclient.repository.AccountRepository;
import pl.edu.wsinf.lukaszwalczak.emailclient.view.AccountCreateView;

@Component
public class AccountCreateController {
    
    @Inject
    private AccountCreateView view;
    @Inject
    private AccountRepository accountRepository;
    @Inject
    private MainWindowController mainWindowController;
    
    public void showWindow() {
        view.show();
    }
    
    public void createAccount(Map<String, String> values) {
        
        System.out.println(values.toString());
        
        Account account = new Account();
        account.setEmailAddress(values.get("email"));
        account.setPassword(values.get("password"));
        account.setUserName(values.get("username"));
        account.setIncomingServer(values.get("incomingServer"));
        account.setIncomingServerPort(Integer.valueOf(values.get("incomingServerPort")));
        account.setIncomingServerProtocol(IncomingProtocol.valueOf(values.get("incomingServerProtocol")));
        account.setIncomingServerAuth(ServerAuth.valueOf(values.get("incomingServerAuth")));
        account.setOutgoingServer(values.get("outgoingServer"));
        account.setOutgoingServerPort(Integer.valueOf(values.get("outgoingServerPort")));
        account.setOutgoingServerProtocol(OutgoingProtocol.valueOf(values.get("outgoingServerProtocol")));
        account.setOutgoingServerAuth(ServerAuth.valueOf(values.get("outgoingServerAuth")));
        accountRepository.save(account);
        mainWindowController.initMainWindowViewLeftPanel();
    }
}
